<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 11] 11.6 Checkboxes and CheckboxGroups</TITLE>
<META NAME="author" CONTENT="Pat Niemeyer and Josh Peck">
<META NAME="date" CONTENT="Tue Jul 22 19:01:12 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Exploring Java">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Exploring Java" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch11_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 11<br>Using and Creating GUI Components</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch11_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="EXJ-CH-11-SECT-6">11.6 Checkboxes and CheckboxGroups</A></h2>

<P CLASS=para>
<A NAME="CH10.CHECK1"></A> <A NAME="CH10.CHECK2"></A>
<P CLASS=para>
A <tt CLASS=literal>Checkbox</tt> is a labeled toggle button. A group of
such toggle buttons can be made mutually exclusive by tethering them
together with a <tt CLASS=literal>CheckboxGroup</tt> object. By now
you're probably well into the swing of things and could easily
master the <tt CLASS=literal>Checkbox</tt> on your own. We'll throw
out an example to illustrate a different way of dealing with the state
of components and to show off a few more things about containers.

<P CLASS=para>

A <tt CLASS=literal>Checkbox</tt> sends item events when it's
pushed, just like a <tt CLASS=literal>List</tt>, a
<tt CLASS=literal>Menu</tt>, or a
<tt CLASS=literal>Choice</tt>. In our last example, we caught the
action events from our choice and menu selections and worked with them
when they happened. For something like a checkbox, we might want to
be lazy and check on the state of the buttons only at some later time,
such as when the user commits an action. It's like filling out a
form; you can change your choices until you submit the form. 

<P CLASS=para>
The following applet, <tt CLASS=literal>DriveThrough</tt>, lets us
check off selections on a fast food menu, as shown in
<A HREF="ch11_03.htm#EXJ-CH-11-FIG-4">Figure 11.4</A>.
<tt CLASS=literal>DriveThrough</tt> prints 
the results when we press the <b>Place
Order</b> button. Therefore, we can ignore all the item
events generated by our checkboxes and listen only for the action
events generated by the button. 

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="EXJ-CH-11-FIG-7">Figure 11.7: The DriveThrough applet</A></h4>


<p>
<img align=middle src="./figs/je1107.gif" alt="[Graphic: Figure 11-7]" width=503 height=182 border=0>

</DIV>

<DIV CLASS=screen>
<P>
<PRE>
import java.awt.*;
import java.awt.event.*;
public class OrderForm extends java.applet.Applet implements ActionListener { 
    Panel condimentsPanel = new Panel();
    CheckboxGroup entreeGroup = new CheckboxGroup();
    public void init() {
        condimentsPanel.add( new Checkbox("Ketchup"));
        condimentsPanel.add( new Checkbox("Mustard"));
        condimentsPanel.add( new Checkbox("Pickles"));
        Checkbox c;
        Panel entreePanel = new Panel();
        entreePanel.add( c = new Checkbox("Beef") );
        c.setCheckboxGroup( entreeGroup );
        entreePanel.add( c = new Checkbox("Chicken") );
        c.setCheckboxGroup( entreeGroup );
        entreePanel.add( c = new Checkbox("Veggie") );
        c.setCheckboxGroup( entreeGroup );
        entreeGroup.setCurrent( c );
        Panel orderPanel = new Panel();
        Button orderButton = new Button("Place Order");
        orderButton.addActionListener( this );
        orderPanel.add( orderButton );
        setLayout( new GridLayout(3, 1) );
        add( entreePanel );
        add( condimentsPanel );
        add( orderPanel );
    }
    public void actionPerformed(ActionEvent e) {
        takeOrder();
    }
    void takeOrder() {
        Checkbox c = entreeGroup.getCurrent();
            System.out.println( c.getLabel() + " sandwich" );
        Component [] components = condimentsPanel.getComponents();
        for (int i=0; i&lt; components.length; i++)
            if ( (c = (Checkbox)components[i]).getState() )
                System.out.println( "With " + c.getLabel() );
        System.out.println("Thank you, drive through...");
    }
}
</PRE>
</DIV>

<P CLASS=para>
<tt CLASS=literal>DriveThrough</tt> lays out two panels, each
containing three checkboxes. The checkboxes in the
<tt CLASS=literal>entreePanel</tt> are tied together through a single
<tt CLASS=literal>CheckboxGroup</tt> object. 
We call their <tt CLASS=literal>setCheckboxGroup()</tt> methods to
put them in a single <tt CLASS=literal>CheckboxGroup</tt> that
makes the checkboxes mutually exclusive. The
<tt CLASS=literal>CheckboxGroup</tt> object is an odd animal. One
expects it to be a container or a component, but it isn't; it's simply
a helper object that coordinates the functionality of the
<tt CLASS=literal>Checkbox</tt> objects. Because a
<tt CLASS=literal>CheckboxGroup</tt> isn't a container, it
doesn't have an <tt CLASS=literal>add()</tt> method. To put a
checkbox into a group, you call the
<tt CLASS=literal>setCheckboxGroup()</tt> method of the
<tt CLASS=literal>Checkbox</tt> class.

<P CLASS=para>
Once a set of checkboxes have been placed in a checkbox group,
only one of the boxes may be checked at a time. In this applet, the
checkbox group forces you to choose a beef, chicken, or veggie entree,
but not more than one. The condiment choices, however, aren't in
a checkbox group, so you can request ketchup, mustard, and pickles on
your chicken sandwich.

<P CLASS=para>
When the <b>Place
Order</b> button is 
pushed, we receive an <tt CLASS=literal>ActionEvent</tt> via
our <tt CLASS=literal>actionPerformed()</tt> method. At this
point, we gather the information in the checkboxes and 
print it. <tt CLASS=literal>actionPerformed()</tt> simply calls
our <tt CLASS=literal>takeOrder()</tt> method, which reads the
checkboxes. We could have saved references to the checkboxes in a number
of ways; 
this example demonstrates two. First, we find out which entree was
selected. 
To do so, we call the <tt CLASS=literal>CheckboxGroup</tt>'s
<tt CLASS=literal>getCurrent()</tt> method.
<tt CLASS=literal>getCurrent()</tt> 
returns the selected <tt CLASS=literal>Checkbox</tt>; we use
<tt CLASS=literal>getLabel()</tt> to extract the entree's name.

<P CLASS=para>
To find out which condiments were selected, we use a more
complicated procedure. The problem is that condiments aren't
mutually exclusive, so we don't have the convenience of a
<tt CLASS=literal>CheckboxGroup</tt>. Instead, we ask the condiments
<tt CLASS=literal>Panel</tt> for a list of its components. The
<tt CLASS=literal>getComponent()</tt> method returns an array of
references to the container's child components. We'll use
this to loop over the components and print the results. We cast each
element of the array back to <tt CLASS=literal>Checkbox</tt> and
call its 
<tt CLASS=literal>getState()</tt> method to see if the checkbox
is on or 
off. Remember that if we were dealing with different types of
components, we could determine what kind of component we had with the
<tt CLASS=literal>instanceof</tt> operator.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch11_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch11_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>PopupMenus</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>ScrollPane and Scrollbars</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
